/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/3/14
 */
import React, {Component, Fragment} from 'react';
import {listAllAssignedPositions} from "../service";
import {connect} from "react-redux";
import {hide, show} from "../../../configs/SharedReducer";
import {searchListByKeyWithTag} from "../../../commons/utils/CommonUtils";
import ToolBar from "../../../commons/components/toolbar/ToolBar";
import SimpleTable from "../../../commons/components/SimpleTable";

class PositionInfo extends Component {
  state = {
    dataSource: [],
    searchData: [],
    searchValue: ""
  }

  componentDidMount() {
    this.getDataSource();
  }

  getDataSource = () => {
    const {userId} = this.props;
    listAllAssignedPositions({employeeId: userId})
      .then(res => {
        this.setState({dataSource: res && res.data ? res.data : []});
      })
      .finally(() => {

      })
  }
  getColumns = () => {
    return [
      {
        title: "代码",
        width: 100,
        dataIndex: "code"
      },
      {
        title: "名称",
        width: 200,
        dataIndex: "name"
      },
      {
        title: "岗位类别",
        width: 150,
        dataIndex: "positionCategory.name"
      },
      {
        title: "组织机构",
        width: 300,
        dataIndex: "organization.namePath"
      }
    ];
  }
  handleQuickSearch = (searchValue) => {
    this.setState({searchValue});
    searchListByKeyWithTag(this.state.dataSource, {keyword: searchValue}, ['code', 'name']);
  }
  searchBtnCfg = () => {
    return {
      quickSearchCfg: {
        style: {width: 250},
        placeholder: "输入代码或名称查询",
        allowClear: true,
        onSearch: (v) => {
          this.handleQuickSearch(v)
        }
      }
    }
  }

  render() {
    const {dataSource, searchValue} = this.state;
    return (
      <Fragment>
        <ToolBar
          searchBtnCfg={this.searchBtnCfg()}
        />
        <SimpleTable
          data={searchValue ? dataSource.filter(item => item.tag === true) : dataSource}
          columns={this.getColumns()}
        />
      </Fragment>
    );
  }
}

export default connect(null, {show, hide})(PositionInfo);
